package com.baichang.taoshunda.controller;

import com.baichang.framework.controller.IController;
import com.baichang.framework.controller.IPageController;
import com.baichang.framework.controller.WebBaseController;
import com.baichang.framework.excelption.ServiceException;
import com.baichang.framework.model.Page;
import com.baichang.taoshunda.ControllerConstants;
import com.baichang.taoshunda.service.CarouselService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by Administrator on 2018/2/8 0008.
 */
@Controller
@RequestMapping(ControllerConstants.BACKGROUNDCAEOUSEL)
public class CarouselWController extends WebBaseController {

    @Autowired
    private CarouselService carouselService;

    /**
     * 跳转审核轮播图
     *
     * @return
     */
    @RequestMapping("/findAuditingCarousl")
    public String findAuditingCarousl() {
        return "taoshunda/auditing/auditingCarousel";
    }

    /**
     * 查询需要审核的广告位
     *
     * @param request
     * @param response
     */
    @RequestMapping("/queryAuditingCarousel")
    public void queryAuditingCarosuel(final HttpServletRequest request, HttpServletResponse response) {
        initPage(request, response, new IPageController() {
            @Override
            public Page initPage(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                String businessName = getString(request, "businessName", "");
                Integer type = getInt(request, "type", 0);
                return carouselService.queryAuditingCarosuel(businessName, type, nowPage, pageSize);
            }
        });
    }

    /**
     * 轮播图上传图片
     *
     * @param request
     * @param response
     */
    @RequestMapping("/addPicture")
    public void addPicture(final HttpServletRequest request, HttpServletResponse response) {
        init(request, response, new IController() {
            @Override
            public Object initData(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                Integer id = getInt(request, "id", 0);
                String picture = getString(request, "picPath", "");
                return carouselService.addPicture(id, picture);
            }
        });
    }

    /**
     * 拒绝审核
     *
     * @param request
     * @param response
     */
    @RequestMapping("/update")
    public void updateCarosuel(final HttpServletRequest request, HttpServletResponse response) {
        init(request, response, new IController() {
            @Override
            public Object initData(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                Integer id = getInt(request, "id", 0);
                return carouselService.update(id);
            }
        });
    }

    /**
     * 查询同意和拒绝的轮播图
     *
     * @param request
     * @param response
     */
    @RequestMapping("/queryCarousel")
    public void queryCarousel(final HttpServletRequest request, HttpServletResponse response) {
        initPage(request, response, new IPageController() {
            @Override
            public Page initPage(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                String name = getString(request, "name", "");
                Integer cityId = getInt(request, "cityId", 0);
                Integer type = getInt(request, "type", 0);
                return carouselService.queryCarousel(name, cityId, type, nowPage, pageSize);
            }
        });
    }

    /**
     * 更换图片
     *
     * @param request
     * @param response
     */
    @RequestMapping("/replacePicture")
    public void replacePicture(final HttpServletRequest request, HttpServletResponse response) {
        init(request, response, new IController() {
            @Override
            public Object initData(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                Integer id = getInt(request, "id", 0);
                String picture = getString(request, "picPath", "");
                return carouselService.replacePicture(id, picture);
            }
        });
    }

    /**
     * 删除
     *
     * @param request
     * @param response
     */
    @RequestMapping("/updateState")
    public void updateIsPosition(final HttpServletRequest request, HttpServletResponse response) {
        init(request, response, new IController() {
            @Override
            public Object initData(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                Integer id = getInt(request, "id", 0);
                Integer link = getInt(request, "link", 0);
                return carouselService.updateCarousel(id, link);
            }
        });
    }
}
